var RE_NUM = new RegExp("[0-9,]", "g");
var RE_NUM_NEG = new RegExp("[0-9,\-]", "g");

function configureInputFilter(tag, className, canBeNegative) {
    var elements = getElementsByTagAndClassName(tag, className);
    for (i=0; i<elements.length; i++) {
        var obj = elements[i];
		obj.canBeNegative = canBeNegative;
        addEvent(obj, "keypress", filterInput);
        addEvent(obj, "keyup", formatContent);
    }
}

function configureInputTextFilter(tag, className) {
    var elements = getElementsByTagAndClassName(tag, className);
    for (i=0; i<elements.length; i++) {
        var obj = elements[i];
       	addEvent(obj, "keyup", filterTextInput);
    }

}
    
function filterTextInput(e) {
    this.value = this.value.toUpperCase();
}

function filterInput(e) {
    var tecla = (window.Event) ? e.which : e.keyCode;
    if (tecla == null) {
        tecla = e.keyCode;
    }
    if((e.keyCode == e.DOM_VK_BACK_SPACE) || (e.keyCode == e.DOM_VK_DELETE) || (e.keyCode == e.DOM_VK_TAB)) {
		return true;
    }
	
    var character = String.fromCharCode(tecla);
    if (this.canBeNegative) {
		return RE_NUM_NEG.exec(character);
	} else {
		return RE_NUM.exec(character);
	}
}

function formatContent(e) {
    var nTecla;
    if(document.layers) { // Nestcape
        nTecla = e.which;
    } else {
        // Internet Explorer / Firefox
        nTecla = e.keyCode;
    }
    sValue = this.value;
    if (sValue == null) {
        return;
    }
	var alreadyHasSignal = hasSignal(sValue);
	sValue = sValue.replace(/[^0-9,]/g, "");
	sValue = sValue.replace(/,([0-9]*),/g, ',$1'); /* Regex para não deixar mais de uma virgula */
	if (this.canBeNegative) {
		if (nTecla == 109 || alreadyHasSignal) {
			sValue = "-" + sValue;
		}
	}
    this.value = sValue;
}


function formatContentNumber(e) {
    var nTecla;
    if(document.layers) { // Nestcape
        nTecla = e.which;
    } else {
        // Internet Explorer / Firefox
        nTecla = e.keyCode;
    }
    fixContentNumber(this);
}

function fixContentNumber(obj) {
	
	obj.value = obj.value;
	sValue = obj.value;
	
	if (sValue == null) {
		return;
	}

	var alreadyHasSignal = hasSignal(sValue);

	regex = new RegExp(",([0-9]{" + obj.lenght + "})[0-9]*", "g");
	
	sValue = sValue.replace(/[^0-9,]/g, "");
	sValue = sValue.replace(/,([0-9]*),/g, ',$1'); /*
													 * Regex para não deixar
													 * mais de uma virgula
													 */
	sValue = sValue.replace(regex, ',$1'); /*
											 * Regex para não deixar mais de
											 * duas casas decimais
											 */
	
	if (this.canBeNegative) {
		if (nTecla == 109 || alreadyHasSignal) {
			sValue = "-" + sValue;
		}
	}
	obj.value = sValue;

}

function hasSignal(sValue) {
	sValue = sValue.replace(/ */g, "");
	for (var i = 0; i < sValue.length - 1; i++) {
		var ch = sValue.charAt(i);
		if (ch == "-") {
			return true;
		}
	}
	return false;
}

function applyMaskNumberByTagAndClassName(tag, className, lenght) {
	  var elements  = getElementsByTagAndClassName(tag, className);
	  for (i=0; i<elements.length; i++) {
	    obj = elements[i];
	    obj.lenght = lenght;
	    addEvent(obj, "keypress", filterInput);
	    addEvent(obj, "keyup", formatContentNumber);
	    fixContentNumber(obj);
	  }
}

/**
 * ****************************************** Aqui estão as funções referentes a
 * data, cpf, cnpj e demais **************************************
 */


function applyMaskByTagAndClassName(tag, className, mask, filterInputRegexp) {
  var elements  = getElementsByTagAndClassName(tag, className);
  for (i=0; i<elements.length; i++) {
    obj = elements[i];
    obj.mask = mask;
    obj.setAttribute("maxlength", mask.length);
    obj.filterInputRegexp = filterInputRegexp;
    addEvent(obj, "keypress", filterInputType);
    addEvent(obj, "keyup", formatMask);
    fixGeneralMask(obj);
  }
}

function applyMaskByTagAndClassNameAndNoMaxLength(tag, className, mask, filterInputRegexp) {
  var elements  = getElementsByTagAndClassName(tag, className);
  for (i=0; i<elements.length; i++) {
    obj = elements[i];
    obj.mask = mask;
    obj.filterInputRegexp = filterInputRegexp;
    addEvent(obj, "keypress", filterInput);
    addEvent(obj, "keyup", formatMask);
    fixGeneralMask(obj);
  }
}

function filterInputType(e) {
  if (this.value.length == this.maxlength) {
    return false;
  }
  var tecla = (window.Event) ? e.which : e.keyCode;
  if (tecla == null) {
    tecla = e.keyCode;
  }
  if((e.keyCode == e.DOM_VK_BACK_SPACE) || (e.keyCode == e.DOM_VK_DELETE) || (e.keyCode == e.DOM_VK_TAB)) {
      return true;
  }
  var character = String.fromCharCode(tecla);
  var myregexp = new RegExp(this.filterInputRegexp, "g");
  var x = myregexp.exec(character);
  if (x) {
    return true;
  }   
  return false;
}

function formatMask(evtKeyPress){
  var i, nCount, sValue, tmpLen, bolMask, sCod, nTecla, tmp, mask;
  
  if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
  } else {
    // Internet Explorer / Firefox
    nTecla = evtKeyPress.keyCode;
  }
  fixGeneralMask(this);
}

function fixGeneralMask(obj) {
  mask = obj.mask;
  obj.value = obj.value;
  sValue = obj.value;

  if (sValue == null) {
    return;
  }

  // Limpa todos os caracteres de formatação que já estiverem no campo.
  sValue = sValue.toString().replace(/[-]/g,"");
  sValue = sValue.toString().replace(/[.]/g,"");
  sValue = sValue.toString().replace(/[\/]/g,"");
  sValue = sValue.toString().replace(/[(]/g,"");
  sValue = sValue.toString().replace(/[)]/g,"");
  sValue = sValue.toString().replace(/[ ]/g,"");
  sValue = sValue.toString().replace(/[,]/g,"");
  sValue = sValue.toString().replace(/[:]/g,"");
	
  var posMask = mask.length - 1;
  var posVal  = sValue.length - 1;
  var sCod    = "";
  while (posVal >= 0) {
    bolMask = ((mask.charAt(posMask) == "-") || (mask.charAt(posMask) == ".") || (mask.charAt(posMask) == "/") || (mask.charAt(posMask) == ":"))
    bolMask = bolMask || ((mask.charAt(posMask) == "(") || (mask.charAt(posMask) == ")") || (mask.charAt(posMask) == " ") || (mask.charAt(posMask) == ","))
    if (bolMask) {
      sCod = mask.charAt(posMask) + sCod;
      posVal++;
    } else {
      if (mask.charAt(posMask) == "9") { // apenas números...
        sCod = sValue.charAt(posVal).replace(/\D/g,"") + sCod;
      } else {
        sCod = sValue.charAt(posVal) + sCod;
      }  
    }
    
    posVal--;
    posMask--;
  }
  
  obj.value = sCod;
}
